package com.stedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.Admin;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.impl.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/admin/adminuser")
public class AdminController {
    @Value("5")
    private  Integer pageSize;
    @Autowired
    private AdminServiceImpl adminService;



    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") int id ) throws SteduException {
        //获取请求参数


        if (adminService.delete(id)) {
            return  RespBean.ok("删除成功~~~");
        } else {
            return RespBean.error("删除失败，请重新删除");
        }
    }

    @PutMapping
    public RespBean update(@RequestBody Admin admin) throws SteduException {
        if (adminService.update(admin)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return   RespBean.error("修改失败，请重新修改");
        }
    }

    @GetMapping("/{id}")
    public RespBean eselectByDid(@PathVariable("id") int id) {
        Admin a= adminService.selectById(id);
        return RespBean.ok("", a);
    }

    @GetMapping
    public RespBean selectPage(Integer pageNum,String uname){
        if (pageNum == null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum ,pageSize);
        List<Admin> carsList=adminService.selectPage(uname);
        PageInfo<Admin> pageInfo=new PageInfo<>(carsList,5);
        return  RespBean.ok("查询成功", pageInfo);
    }

}
